package net.parostroj.timetable.gui.dialogs;

import net.parostroj.timetable.gui.actions.execution.ActionUtils;
import net.parostroj.timetable.gui.utils.ResourceLoader;
import net.parostroj.timetable.model.GrafikonException;
import net.parostroj.timetable.model.Script;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Dialog for editing scripts.
 *
 * @author jub
 */
public class ScriptDialog extends javax.swing.JDialog {

    private static final Logger LOG = LoggerFactory.getLogger(ScriptDialog.class);

    private Script selectedScript;

    /** Creates new form ScriptDialog */
    public ScriptDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public Script getScript() throws GrafikonException {
        return scriptEditBox.getScript();
    }

    public void setScript(Script script) {
        scriptEditBox.setScript(script);
    }

    public void setEditorSize(int columns, int rows) {
        scriptEditBox.setColumns(columns);
        scriptEditBox.setRows(rows);
    }

    public Script getSelectedScript() {
        return selectedScript;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scriptEditBox = new net.parostroj.timetable.gui.components.ScriptEditBox();
        javax.swing.JPanel buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(ResourceLoader.getString("script.editing")); // NOI18N

        scriptEditBox.setColumns(60);
        scriptEditBox.setRows(15);
        scriptEditBox.setScriptFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        getContentPane().add(scriptEditBox, java.awt.BorderLayout.CENTER);

        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        okButton.setText(ResourceLoader.getString("button.ok")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        cancelButton.setText(ResourceLoader.getString("button.cancel")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(cancelButton);

        getContentPane().add(buttonPanel, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        try {
            selectedScript = this.getScript();
            this.setVisible(false);
        } catch (GrafikonException e) {
            LOG.error("Error creating script.", e);
            String message = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
            ActionUtils.showError(message, this);
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    @Override
    public void setVisible(boolean b) {
        if (b)
            selectedScript = null;
        super.setVisible(b);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton okButton;
    private net.parostroj.timetable.gui.components.ScriptEditBox scriptEditBox;
    // End of variables declaration//GEN-END:variables
}
